package com.bubbleby.social.launch.client;

import java.util.Random;

import com.bubbleby.social.launch.client.external.FacebookWrapper;
import com.bubbleby.social.launch.client.resources.I18nConstants;
import com.bubbleby.social.launch.client.resources.I18nMessages;
import com.bubbleby.social.launch.client.resources.MainResourceBundle;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SocialLaunch implements EntryPoint, ResizeHandler {

	/** The Constant ACCESS_KEY_CHARACTERS. */
	private static final String ACCESS_KEY_CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_";

	/** Random number constant. */
	private static final Random RANDOM = new Random();

	/** The main resource bundle. */
	public static MainResourceBundle resourceBundle = (MainResourceBundle) GWT.create(MainResourceBundle.class);

	/** The messages. */
	public static I18nMessages messages = (I18nMessages) GWT.create(I18nMessages.class);

	/** The constants. */
	public static I18nConstants constants = (I18nConstants) GWT.create(I18nConstants.class);

	/**
	 * Create a remote service proxy to talk to the server-side Register
	 * service.
	 */
	private final RegisterServiceAsync registerService = GWT.create(RegisterService.class);

	/** The launch Page UI Binder. */
	LaunchPage launchPage = new LaunchPage(registerService);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		FacebookWrapper.initFacebook();
		resourceBundle.generalCSS().ensureInjected();

		RootPanel.get("main").add(launchPage);
		Window.addResizeHandler(this);
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.event.logical.shared.ResizeHandler#onResize(com.google
	 * .gwt.event.logical.shared.ResizeEvent)
	 */
	@Override
	public void onResize(ResizeEvent event) {
		launchPage.changeBgImage(event.getWidth(), event.getHeight(), false);
	}

	/**
	 * Creates the access key handed on through mail, facebook or tweets.
	 * 
	 * @return the hash
	 */
	public static String createAccessKey() {
		StringBuffer key = new StringBuffer();
		for (int i = 0; i < 6; i++) {
			key.append(ACCESS_KEY_CHARACTERS.charAt(RANDOM.nextInt(ACCESS_KEY_CHARACTERS.length())));
		}
		return key.toString();
	}

	/**
	 * Track event in Google Analytics.
	 * 
	 * @param category
	 *            the Google Analytics category
	 * @param action
	 *            the Google Analytics action
	 * @param label
	 *            the Google Analytics label
	 */
	public static native void trackEventInAnalytics(String category, String action, String label) /*-{
		$wnd._gaq.push( [ '_trackEvent', category, action, label ]);
	}-*/;

	/**
	 * Track event in Google Analytics.
	 * 
	 * @param category
	 *            the Google Analytics category
	 * @param action
	 *            the Google Analytics action
	 * @param label
	 *            the Google Analytics label
	 * @param value
	 *            the Google Analytics value
	 */
	public static native void trackEventInAnalytics(String category, String action, String label, String value) /*-{
		$wnd._gaq.push(['_trackEvent', category, action, label, value]);
	}-*/;
}
